package org.jeecg.modules.bobo.desform.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.bobo.common.util.DateUtil;
import org.jeecg.modules.bobo.common.util.JsonUtils;
import org.jeecg.modules.bobo.common.util.Result;
import org.jeecg.modules.bobo.desform.api.vo.SubmitDesformVo;
import org.jeecg.modules.bobo.desform.entity.LbDesform;
import org.jeecg.modules.bobo.desform.entity.LbDesformData;
import org.jeecg.modules.bobo.desform.service.ILbDesformDataService;
import org.jeecg.modules.bobo.desform.service.ILbDesformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Locale;

/**
 * @program: jeecg-boot-parent
 * @description: 动态表单api
 * @author: 三千霜
 * @create: 2021-04-09 09:49
 */
@Slf4j
@RestController
@RequestMapping("/api/desform")
@Api(tags = "动态表单api")
public class DesformApiController {

    @Autowired
    private ILbDesformService lbDesformService;

    @Autowired
    private ILbDesformDataService lbDesformDataService;

    @RequestMapping(value = "/getDesformList", method = RequestMethod.GET)
    @ApiOperation("列出调查问卷")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "表单类型",example = "")
    })
    public Result<List<LbDesform>> getDesformList(@RequestParam(name="type") String type){
        LambdaQueryWrapper<LbDesform> eq = new QueryWrapper<LbDesform>().lambda().eq(LbDesform::getDesformType, type);
        eq.gt(LbDesform::getEndTime, DateUtil.getDate());
        eq.lt(LbDesform::getStartTime, DateUtil.getDate());
        eq.eq(LbDesform::getStatus,"1");
        eq.orderByDesc(LbDesform::getCreateTime);
        List<LbDesform> list = lbDesformService.list(eq);
        if (list!=null&&list.size()>0){
            return Result.success(list);
        }
        return Result.error("暂无数据");
    }

    @RequestMapping(value = "/getDesform", method = RequestMethod.GET)
    @ApiOperation("获取动态表单配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "表单id",example = ""),
            @ApiImplicitParam(name = "type",value = "表单类型",example = "")
    })
    public Result<LbDesform> getDesform(@RequestParam(name="id") String id, @RequestParam(name="type",required = false) String type){
        LambdaQueryWrapper<LbDesform> eq = new QueryWrapper<LbDesform>().lambda();
        if (StringUtils.isNotBlank(type)){
            eq.eq(LbDesform::getDesformType, type);
        }
        eq.gt(LbDesform::getEndTime, DateUtil.getDate());
        eq.lt(LbDesform::getStartTime, DateUtil.getDate());
        eq.eq(LbDesform::getId,id);
        eq.eq(LbDesform::getStatus,"1");
        eq.orderByDesc(LbDesform::getCreateTime);
        LbDesform one = lbDesformService.getOne(eq);
        if (one!=null){
            return Result.success(one);
        }
        return Result.error("暂无数据");
    }


    @ApiOperation(value="提交问卷", notes="提交问卷")
    @PostMapping(value = "/submitDesform")
    public Result<String>  submitDesform(@RequestBody @Validated SubmitDesformVo submitDesformVo ) {
        LbDesform lbDesform = lbDesformService.getById(submitDesformVo.getDesformId());
        LbDesformData lbDesformData=new LbDesformData();
        lbDesformData.setDesformId(lbDesform.getId());
        if(StringUtils.isNotBlank(lbDesform.getDesformCode())){
            lbDesformData.setDesformCode(lbDesform.getDesformCode());
        }
        lbDesformData.setDesformName(lbDesform.getDesformName());
        lbDesformData.setDesformDataJson(submitDesformVo.getDesformDataJson());
        if(lbDesformDataService.save(lbDesformData)) {
            return Result.success("提交成功");
        }
        return Result.error("提交失败");
    }

}
